package server.model.connections;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;


public class ConnectionListener implements Runnable{

	private boolean finished;
	private Integer listeningPort;

	public ConnectionListener(Integer port){
		this.finished=false;
		this.listeningPort=port;
	}

	@Override
	public void run() {
		System.out.println("[ConnectionListener/INFO] new Listener on port: " + this.listeningPort + " started at" + new Date());
		try {
			ServerSocket svr=new ServerSocket(this.listeningPort);
			while(!this.finished){
				Socket newClient=svr.accept();
				new Thread(new ConnectionManager(newClient)).start();
			}
		} catch (IOException e) {
			e.printStackTrace(System.err);
		}
	}
}
